package java_util.web;

import java_util.coo_util.Log.C_Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class C_WebRequest {


    public static String S_Get(String url) {
        C_Log.Log("发送web.get请求...." + url);
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
        HttpGet httpGet = new HttpGet(url);
        try {
            // 执行get请求
            HttpResponse httpResponse = closeableHttpClient.execute(httpGet);
            // 获取响应消息实体
            HttpEntity entity = httpResponse.getEntity();
            return EntityUtils.toString(entity, "UTF-8");
        } catch (Exception e) {
            C_Log.LogErrorInfo("get请求错误:", e, true);
        } finally {
            releaseHttp(closeableHttpClient);
        }
        return "";
    }

    /**
     * 释放连接
     */
    private static void releaseHttp(CloseableHttpClient client) {
        try {
            client.close();
        } catch (IOException e) {
            C_Log.LogErrorInfo("releaseHttp is error...", e, true);
        }
    }


    /**
     * post求请
     */
    public static String S_Post(String url, Map<String, Object> params) {
        C_Log.Log("发送web.post请求...." + url);
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
        // post请求
        HttpPost httpPost = new HttpPost(url);
        // 创建参数队列
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        Set<String> keySet = params.keySet();
        for (String key : keySet) {
            formparams.add(new BasicNameValuePair(key, params.get(key).toString()));
        }
        try {
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
            httpPost.setEntity(entity);
            // post请求
            HttpResponse httpResponse = closeableHttpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            if (httpEntity != null) {
                return EntityUtils.toString(httpEntity, "UTF-8");
            }
        } catch (Exception e) {
            C_Log.LogErrorInfo("post请求错误:", e, true);
        } finally {
            try {
                closeableHttpClient.close();
            } catch (IOException ex2) {
                C_Log.LogErrorInfo("post请求错误55", ex2, true);
            }
        }
        return "";
    }

}
